﻿using Cofe.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Service;
using Cofe.Core.EntryType;
using Cofe.Core.Logger;
using Cofe.Core.Utils;
using Cofe.Core.Implements;
using Cofe.Core.Security;

namespace Cofe.Core.Property
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
    public class EntryTypeVolumeFactory : ISystemVolumeFactory, INotifyImported
    {
        #region Defines

        #endregion

        #region Methods

        public Task<IVolume> ConstructAsync(IVolumeRegistrar volumeRegistrar, string volumeId, string paramString)
        {
            var volumeInfo = new VolumeInfo() { FactoryId = FactoryId, ParamString = paramString, VolumeId = EntryTypes.VolumeKey };

            IVolume result = new Volume(volumeInfo, ParamParser, new EntryTypeDirectoryLister());

            return Task.FromResult<IVolume>(result);

        }

        public void OnImported(IServiceLocater serviceLocater)
        {
            var volumeRegistrar = serviceLocater.TryFindService<IVolumeRegistrar>();
            if (volumeRegistrar != null)
                VolumeFE.MapAsync("EntryType", EntryTypes.VolumeKey, "").Wait();

            serviceLocater.FindService<ISecurityManager>().ServicePermissionPolicies
               .Add(VolumePermissionPolicy.ForAnyRole(EntryTypes.VolumeKey, PermissionType.All));
        }

        #endregion

        #region Data

        private IEventHub _eventHub = null;
        private IParamParser _paramParser = new ParamParser();

        #endregion


        #region Public Properties

        public IEventHub EventHub
        {
            get { return _eventHub ?? CofeServices.ServiceLocater.FindService<IEventHub>(); }
            set { _eventHub = value; }
        }

        public IParamParser ParamParser
        {
            get { return _paramParser ?? CofeServices.ServiceLocater.FindService<IParamParser>(); }
            set { _paramParser = value; }
        }


        public string FactoryId
        {
            get { return "EntryType"; }
        }


        #endregion


    }
}
